/**
  * filename:       adm-facade.js
  * description:    Application Layout
  */

Ext.namespace("Layout");
Layout.base = function(){
    Ext.QuickTips.init();
	
	var cfgStore = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({url: '/admin/node-configs/'}),
	    restful: true,
	    reader: new Ext.data.JsonReader({root: 'data',totalProperty: '',idProperty: 'Id'},[ 
		    {name: 'Id', type: 'int', mapping: 'Id'},
		    {name: 'Name', type: 'string', mapping: 'Name'},
			{name: 'Controller', type: 'string', mapping: 'Controller'}
		])
	});
    cfgStore.load();
	
	var layoutStore = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({url: '/admin/layout/'}),
	    restful: true,
	    reader: new Ext.data.JsonReader({root: 'data',totalProperty: '',idProperty: 'Id'},[ 
		    {name: 'Id', type: 'int', mapping: 'Id'},
			{name: 'Layout', type: 'string', mapping: 'Layout'},
		    {name: 'Name', type: 'string', mapping: 'Name'}
		])
	});
    layoutStore.load();
	
	var tab =  new Ext.TabPanel({
    	margins: '0 0 5 0',
        region: 'center', // a center region is ALWAYS required for border layout
        deferredRender: false,
        enableTabScroll: true,
        activeTab: 0,     // first tab initially active
        items: [ {
            contentEl: 'center2',
            title: 'Информация',
            autoScroll: true,
            closeable: true
        }]
    });
	
	var statusBar = new Ext.ux.StatusBar({
        region: 'south',
        text: 'Готово',
        iconCls: 'x-status-valid',
        defaultText: '', 
        defaultIconCls: ''
		,reqFailed: function(rsp){statusBar.setStatus({text: 'Ошибка. Не удалось выполнить операцию',iconCls: 'x-status-invalid',clear: true});}
		,reqSuccess: function(rsp){var r = eval("("+rsp.responseText+")"); statusBar.setStatus({text: r.msg,iconCls: r.success?'x-status-valid':'x-status-invalid',clear: true});}
    });
	
	var tree = new Nippy.Components.SiteTree({layoutStore: layoutStore, cfgStore: cfgStore, tab: tab, sb:statusBar, root: rootNode});
	
	function createPropertiesGrid(){
		var g = new Ext.grid.PropertyGrid({
			title: 'Настройки',
			closable: true,
			width: 300,
			autoHeight: true,
			viewConfig : {
				forceFit: true,
				scrollOffset: 2 // the grid will never have scrollbars
			}
			,buttons: [{text: 'Сохранить',scope:g,handler: function(){
					Ext.Ajax.request({
						url: '/admin/settings/'
						,method: 'PUT'
						,jsonData: g.getSource()
						,success: statusBar.reqSuccess
						,failure: statusBar.reqFailed
					});
				}}
				,{text: 'Закрыть',scope:g,handler: function(){tab.remove(g)}}]
		});
		return g;
	}
	
	function showSettingsGrid(){
		Ext.Ajax.request({
			url: '/admin/settings/'
			,success: function(rsp){
				var t = eval("("+rsp.responseText+")");
				var g = createPropertiesGrid();
				g.setSource(t);
				tab.add(g).show();
			}
			,failure: statusBar.reqFailed
		});
	}
    var b = ['->',{text: '<a href="/admin/login/logout/">Выход</a>', xtype: 'tbtext'}];
    return {
		tab: tab,
        init: function(){
    		new Ext.Viewport({
		        layout: 'border',
		        items: [
				new Ext.Toolbar({
					region: 'north',
					height: 32,
					items: this.btns==undefined?b : this.btns.concat(b)
				})
				,{
		            region: 'west',
		            //id: 'west-panel',
		            title: 'Дерево сайта',
		            split: true,
		            width: 200,
		            minSize: 175,
		            maxSize: 400,
		            collapsible: true,
		            margins: '0 0 5 5',
					tbar: [ {text: 'Сайт',
							menu: new Ext.menu.Menu({
								items:[
									{text: 'Обновить .htaccess', scope:tree, handler: tree.refreshUrl}
									,{text: 'Обновить sitemap', scope:tree, handler: tree.updateSitemap}
									,'-'
									,{text: 'Настройки', scope:tree, handler: showSettingsGrid}
								]
							})
						},'-',
						{text: 'Обновить', scope:tree, handler: tree.refreshTree},
						{text: 'Очистить кеш', scope:tree, handler: tree.clearCache}
					],
		            items: [tree],
					autoScroll: true
		        }
		        ,statusBar
		        ,tab]
		    });
        }
    };
}();
Ext.onReady(Layout.base.init, Layout.base);
